package com.example.s09thymeleaf.controller;

import com.example.s09thymeleaf.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {
    @GetMapping("/user")
    public  String showUserPage(){return "user";}
    @PostMapping("/user1")
    @ResponseBody
    //接收前端页面的第一种的方法：方法的参数名称要跟网页中的表单元素的name属性值一致
    public String user1(String name, int age){
        String msg="姓名"+name+",年龄:"+age;
        return msg;
    }
    @PostMapping("/user2")
    @ResponseBody
    //接收前端页面数据的第二种方法：@RequestParam的name属性值要跟网页中的表单元素的name属性值一致
    public String user2(@RequestParam(name = "name",required = false,defaultValue ="Mike" )String name2,
                @RequestParam(name = "age",required = false,defaultValue = "20")  int age2){
        String msg="姓名"+name2+",年龄:"+age2;
        return msg;
    }
    @PostMapping("/user3")
    @ResponseBody
    //接收前端页面的第三种方法：通过HttpServletRequest对象接收数据
    public  String user3(HttpServletRequest request){
        String name=request.getParameter("name");
        String age=request.getParameter("age");
        String msg ="姓名"+name+",年龄:"+age;
        return msg;

    }
    @PostMapping("/user4")
    @ResponseBody
    //接收前端页面的第四种方法：通过类对象进行接收，类对象的属性值要跟表单的属性值名称一致
    public  String user4(Student student){
        String name=student.getName();
        int age=student.getAge();
        String msg ="姓名"+name+",年龄:"+age;
        return msg;

    }

}
